gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\bayes\bayesdemo3.m

    % BAYESDEMO3 Bayesian risk and minimax strategy.
%


% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac

% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz

% Modifications:
% 1-Nov-2001, V.Franc 

echo on;

% Sets up parameters of the conditional PDFs which have 
% one-dimensional Gaussian distribution.

% p(x|k=1)   for the 1st class
m1=[25];     % mean value
c1=[10];     % variance

% p(x|k=2)   for the 2nd class
m2=[10];     % mean value 
c2=[5];      % variance

pause;  % press anykey


% Computation of values of the bayesian risk with respect
% to the changing aprior probability.

pause;  % press anykey

P1 = 0:0.05:1;      % range of values for the aprior probability

Risk=zeros(1,length(P1));      % Bayesian risk
Eps1=zeros(1,length(P1));      % integral of p(x|k=1) over X2 (class 2)
Eps2=zeros(1,length(P1));      % integral of p(x|k=2) over X1 (class 1)
InterX1 = cell(1,length(P1));  % X = X1 U X2 where X = (-inf,inf)

cnt=1;      

for p1=P1,
   
   % Computation of the Bayesian risk, integrals of p(x|k=1,2) over 
   % X1 and X2 and factorisation of the feature space X (real axis) 
   % onto X1 a X2. The classifier assigns patterns from the subspace 
   % X1 to the 1st class and the patterns from X2 to the 2nd class.
   [risk,eps1,eps2, interX1] = bayeserr(p1,m1,m2,c1,c2);
   
   % Saves results.
   Risk(cnt) = risk;
   Eps1(cnt) = eps1;
   Eps2(cnt) = eps2;
   InterX1{cnt} = interX1;   
   
   cnt=cnt+1; 
   
   echo off;
end

echo on;


% Finds the minimax strategy, i.e. the factorization of X when
% Eps1(p1) = Eps2(p1) and the Bayesian risk is maximal.

pause;  % press anykey
echo off;

[tmp, index]= min( abs(Eps1-Eps2) );

fprintf(1, '\nThe Bayesian risk for minimax strategy is %.3f .\n', ...
    Risk(index) );
fprintf(1, 'Minimax strategy: 1st class ~ subset X1 = ');
if size(InterX1{index}) == 2,   % one interval
   fprintf(1,'(%.3f, %.3f).\n',Rozklad{index}(1), Rozklad{index}(2));
else  % two intervals 
   fprintf(1,'(%.3f, %.3f) U (%.3f, %.3f).\n',...
      InterX1{index}(1), InterX1{index}(2),...
      InterX1{index}(3), InterX1{index}(4));
end
fprintf(1, '                  2nd class ~ subset X2 = (-inf, inf ) / X1\n');


% Plots the Bayesian risk and values of integrals Eps1 and Eps2
% with respect to the aprior probaility p1.

figure;
hold on;
title(['Bayesian risk (red), \epsilon_1=\int_{L2}p(x|k=1)dx (green),',...
       ' \epsilon_2=\int_{L1}p(x|k=2)dx (blue)']);
xlabel('Aprior probability p1');
ylabel('\epsilon_1,\epsilon_2');

plot(P1,Risk,'r');
plot(P1(2:end-2),Eps1(2:end-2),'g'); 
plot(P1(2:end-2),Eps2(2:end-2),'b');


% Plots teh Bayesian risk and the risk for minimax strategy. Next,
% it plots Bayesian risk for fixed strategy (for given p1) with 
% respect to changing real aprior probability.

figure;
hold on;
title('Risk: Bayesian (red), Minimax (blue), fixed startegy (black)');
xlabel('Aprior probability p1');
ylabel('Risk');
plot(P1,Risk,'r');

% level of the risk of minamx strategy
plot([0,1], [Risk(index),Risk(index)],'b--');


% Plots the risk for fixed a strategy.

p1=0.2;        % startegy fixed for this p1.

% Risk = p1*(eps1-eps2) + eps2, Risk(p1=0)=eps2 a Risk(p1=1)=eps1.
R0=Eps2(find(P1==p1));            % risk at point p1=0  
R1=Eps1(find(P1==p1));            % --//--        p1=1

plot([0,1],[R0,R1],'k');          %  absscisa [0,R0] a [1,R1]